<script lang="ts" setup>
import { ref } from 'vue'

// 在线支付弹层
const paymentPopup = ref<UniHelper.UniPopupInstance>({} as UniHelper.UniPopupInstance)

/** 打开弹层 */
const open = () => {
  paymentPopup.value?.open?.()
}

/** 关闭弹层 */
const close = async () => {
  paymentPopup.value?.close?.()
}

// 开放关闭和显示弹层的方法
defineExpose({ open, close })

// 接收父组件传递的金额
const props = defineProps<{
  account: number
  orderId: string
}>()
console.log('props', props)
// 支付方式
const paymentMethod = ref([
  {
    name: '微信支付',
    icon: '/static/images/wechatpay-icon.png',
  },
  // #ifndef APP
  {
    name: '支付宝支付',
    icon: '/static/images/alipay-icon.png',
  },
  // #endif
])

const isActive = ref<number>(0)
// 自定义事件
const emit = defineEmits<{
  change: [index: number]
  confirm: [index: number]
  close: []
}>()
const handleChange = (index: number) => {
  isActive.value = index
  emit('change', index)
}
</script>
<template>
  <uni-popup :is-mask-click="false" @mask-click="emit('close')" ref="paymentPopup" type="bottom">
    <view class="payment-container">
      <view class="payment-header">
        <text class="title">选择支付方式</text>
        <uni-icons
          class="uni-icons-close"
          size="18"
          color="#333"
          type="closeempty"
          @click="emit('close')"
        />
      </view>
      <view class="order-amount">¥{{ props.account }} </view>
      <uni-list :border="false">
        <uni-list-item
          v-for="(item, index) in paymentMethod"
          :key="item.name"
          :title="item.name"
          :thumb="item.icon"
          clickable
          :class="{ active: isActive === index }"
          @click="handleChange(index)"
        >
          <template #footer>
            <radio color="#16C2A3" :checked="isActive === index" :disabled="isActive !== index" />
          </template>
        </uni-list-item>
      </uni-list>
      <button class="uni-button" @click="emit('confirm', isActive)">立即支付</button>
    </view>
  </uni-popup>
</template>

<style lang="scss">
.payment-container {
  min-height: 400rpx;
  border-radius: 30rpx 30rpx 0 0;
  background-color: #fff;
  padding: 10rpx 30rpx 40rpx;

  .payment-header {
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
    margin-bottom: 20rpx;
    font-size: 32rpx;
    color: #333;
    position: relative;
  }

  .uni-icons-close {
    position: absolute;
    top: 2rpx;
    right: 0;
  }

  .order-amount {
    padding: 10rpx 0 10rpx;
    text-align: center;
    font-size: 40rpx;
    color: #333;
  }

  :deep(.uni-list-item__container) {
    padding: 40rpx 0 !important;
  }

  :deep(.uni-list-item--hover) {
    background-color: #fff !important;
  }

  :deep(.uni-list-item__icon) {
    margin-right: 0;
  }

  .uni-button {
    margin-top: 40rpx;
  }
}
</style>
